<template>
        <el-container class="home-container">
            <!-- 头部区域 -->
            <el-header>
                <div>
                    <img src="../assets/heima.png"/>
                    <span>电商后台管理系统</span>
                </div>
                <el-button type="info" @click="loginOut">退出</el-button>
            </el-header>
            <!-- 页面主体区域 -->
            <el-container>
                <!-- 侧边栏 -->
                <el-aside :width="isCollapse?'64px':'200px'">
                    <!-- 侧边栏菜单区域: （menu属性； :unique-opened="true" 只保持一个子菜单的展开，也可只写成 unique-opened；router：启用该模式会在激活导航时以 index 作为 path 进行路由跳转）-->
                    <el-menu background-color="#333945" text-color="#fff" active-text-color="#379DFF" :unique-opened="true" :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
                    <div class="toggle-button" @click="toggleCollapse">目录</div>
                    <!-- 一级菜单 -->
                    <el-submenu :index="subItem.id+''" v-for="subItem in menulist" :key="subItem.id" >
                        <!-- 一级菜单模板区域 -->
                        <template slot="title">
                        <!-- 图标 -->
                        <i :class="iconsObj[subItem.id]"></i>
                        <!-- 文本 -->
                        <span>{{subItem.authName}}</span>
                        </template>

                        <!-- 二级菜单:item.id+'' 将数值转成字符串; item.path:路由跳转路径 -->
                        <el-menu-item :index="'/'+item.path" v-for="item in subItem.children" :key="item.id" @click="saveNavState('/'+item.path)">
                            <template slot="title">
                            <!-- 图标 -->
                            <i class="el-icon-menu"></i>
                            <!-- 文本 -->
                            <span>{{item.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                    </el-menu>
                </el-aside>
                <!-- 右侧内容主体 -->
                <el-main>
                    <!-- 路由占位 -->
                    <router-view></router-view>
                </el-main>
            </el-container>
        </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      iconsObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-lock_fill',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      // 是否折叠
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginOut () {
      // 清空token
      window.sessionStorage.clear()
      // 跳转到登录页
      this.$router.push('/login')
    },
    // 获取所有菜单：get获取到promise函数
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    // 折叠菜单
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="scss" scoped>
.home-container{
    height: 100%;
}
.el-header{
    background: #31383B;
    display: flex;
    justify-content: space-between;
    color: #fff;
    font-size:20px;
    padding-left:0px;
    align-items: center; // 设置了flex容器的对齐方式:stretch center flex-start flex-end baseline initial inherit
    div{
       display: flex;
       align-items: center;
       span{
           margin-left: 15px;
       }
    }
}
.el-aside{
    background: #333945;
    .toggle-button{
        text-align: center;
        color: #fff;
        background: #4F596A;
        font-size: 15px;
    }
    .el-menu{
        border-right: none;
    }
}
.el-main{
    background:#E7ECEF;
}
.iconfont{
    margin-right: 10px;
}
</style>
